<?php
/**
 * 
 *
 * @author Rosanna Paberli Jimenez Rosario
 * @since 20-marzo-2011
 *
 */

class ObjetoCompania extends Controller
{
	
	public function __construct()
	{
		parent::Controller();
		//$this->load->library('chconvertir');
		$this->load->model(array('objetocompaniamodel'));
	}
	
	
    public function index()
    {
    	$this->load->view('main',array('content_page' => 'objetocompania_list', 'menutop' => 0));	
    }
    
    
    public function data()
    {
    	
    	 $page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
		 $sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'id_objeto'; // Columna para el order by
		 $sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'desc'; // Orden (asc|desc)
		 $limit = $this->input->post('rows') ? $this->input->post('rows') : 30;

		 $start = $limit * $page - $limit;
		     if($start < 0) $start = 0;
  
		      header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		      header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		      header("Cache-Control: no-cache, must-revalidate" );
		      header("Pragma: no-cache" );
		     
		
	    $records = $this->objetocompaniamodel->loadRegistros($start, $limit, $sortname, $sortorder);
	    
		$totalRecords = count($records);
		

		 $totalPages = ceil($totalRecords/$limit);
		     if ($page > $totalPages) 
		       $page = $totalPages;

		    $json_output = array(
		            'page'		=> $page,
		            'total'		=> $totalPages,
		            'records'	=> $totalRecords,
		            'rows'		=> array()
		        );

		    if($records != false){
			     foreach($records as $objetos){
			     					  
				     $arr = $json_output['rows'][] = array(
				          'idObjeto'              => $objetos->getIdObjeto(),
				          'tipoObjeto'            => $objetos->getTipoObjeto(),
				     	  'descripcion'           => $objetos->getDescripcionObjeto()
				          
				      );
			      }
		      }

		 echo json_encode($json_output);
    }
    
    
    public function add()
    {
    	 $this->load->view('main',array('content_page' => 'objetocompania_add', 'menutop' => 0));
    }
    
    public function edit($id)
	{
		$registro = $this->objetocompaniamodel->loadRegistrosById($id);
   		$this->load->view('main',array('content_page' => 'objetocompania_edit',
   		 							   'menutop'      => 0, 
   			 						   'objeto'       => $registro));
	}
	
    public function delete()
	{
		$id = $this->input->post('delete_rows');
   		$this->objetocompaniamodel->eliminar($id);
	}
	
    public function save()
    {
    	
    	$data = array("tipo_objeto"        => $this->input->post("tipoObjeto"),
    				  "descripcion_objeto" => $this->input->post("descripcion"));
    	
    	if ($this->input->post('idObjeto') == false) {
   	   	   if ($this->objetocompaniamodel->agregar($data) == true) {
   	   	   	   $mensaje = "El resgistro se agrego exitosamente.";
   	   	   	   $router = true;
   	   	   }else{
   	   	       $mensaje = "Hubo un error...";
   	   	       $router = false;
   	   	   }
   	   	  
   	   }else{
   	       if ($this->objetocompaniamodel->editar($data, $this->input->post('idObjeto')) == true) {
   	   	   	   $mensaje = "El resgistro se edito exitosamente";
   	   	   	   $router = true;
   	   	   }else{
   	   	       $mensaje = "Hubo un error...";
   	   	       $router = false;
   	   	   }
   	   }
   	   
   	   $this->session->set_flashdata('error_title',  $mensaje);
   	   $this->session->set_flashdata('error_router', $router);
   	   redirect('objetocompania');
   	   
    }
	
}